<template>
  <div class="login-container">
    <el-form ref="loginForm"
             :model="loginForm"
             :rules="loginRules"
             class="login-form"
             autocomplete="on"
             label-position="left">
      <div class="title-container">
        <h3 class="title">Login Form</h3>
      </div>

      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon name="user" />
        </span>
        <el-input ref="username"
                  v-model="loginForm.username"
                  name="username"
                  type="text"
                  placeholder="username" />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon name="password" />
        </span>
        <el-input :key="passwordType"
                  ref="password"
                  v-model="loginForm.password"
                  :type="passwordType"
                  placeholder="password"
                  name="password"
                  autocomplete="on"
                  @keyup.enter.native="handleLogin" />
        <span class="show-pwd"
              @click="showPwd">
          <svg-icon :name="passwordType === 'password' ? 'eye-off' : 'eye-on'" />
        </span>
      </el-form-item>

      <el-button :loading="loading"
                 type="primary"
                 style="width:100%; margin-bottom:30px;"
                 @click.native.prevent="handleLogin">Sign in</el-button>

      <div style="position:relative">
        <div class="tips">
          <span>username: admin为admin，其余账号为public</span>
          <span>password: any</span>
        </div>
      </div>
    </el-form>
  </div>
</template>
<script lang='ts'>
import { Component, Vue } from 'vue-property-decorator'
import { Form, Input } from 'element-ui'
import { UserModule } from '@/store/modules/user'
import globalInterface from '@/common/global'

interface loginParams {
  username: string;
  password: string
}
@Component({
  name: 'Login'
})
export default class Login extends Vue {
  private loginForm: loginParams = {
    username: '',
    password: ''
  }

  private loginRules: globalInterface.IObject = {
    username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
    password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
  }

  private loading = false
  private passwordType = 'password'
  showPwd () {
    if (this.passwordType === 'password') {
      this.passwordType = ''
    } else {
      this.passwordType = 'password'
    }
    this.$nextTick(() => {
      (this.$refs.password as Input).focus()
    })
  }

  handleLogin () {
    (this.$refs.loginForm as Form).validate((valid: boolean) => {
      if (valid) {
        this.loading = true
        UserModule.Login(this.loginForm).then(res => {
          const redirect: any = this.$route.query.redirect
          // 重定向对象不存在则返回顶层路径
          this.$router.replace(redirect || '/')
          this.loading = false
        })
      } else {
        return false
      }
    })
  }
}
</script>
<style lang="scss">
// References: https://www.zhangxinxu.com/wordpress/2018/01/css-caret-color-first-line/
@supports (-webkit-mask: none) and (not (cater-color: $loginCursorColor)) {
  .login-container .el-input {
    input {
      color: $loginCursorColor;
    }
    input::first-line {
      color: $lightGray;
    }
  }
}
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      height: 47px;
      background: transparent;
      border: 0px;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $lightGray;
      caret-color: $loginCursorColor;
      -webkit-appearance: none;
      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $loginBg inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>
<style lang="scss" scoped>
.login-container {
  position: relative;
  height: 100%;
  width: 100%;
  // overflow: hidden;
  // background-color: $loginBg;
  // background-image: url("../../../public/images/ts-vue-login-banner01.jpeg");
  // background-size: cover;
  // background-position: center center;
  // background-repeat: no-repeat;
  // background-attachment: fixed;
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $darkGray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      color: $lightGray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $darkGray;
    cursor: pointer;
    user-select: none;
  }
}
.login-container::before {
  background-image: url('../../../assets/images/login_bai.jpg');
  background-size: cover;
  background-position: center center;
  background-repeat: no-repeat;
  background-attachment: fixed;
  filter: alpha(opacity=80);
  -moz-opacity: 0.8;
  -khtml-opacity: 0.8;
  opacity: 0.8;
  content: '';
  display: block;
  z-index: -1;
  height: 100%;
  width: 100%;
  position: absolute;
  //一定要设置position:absolute,这样才能设置z-index，让背景处于内容的下一层
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}
</style>
